package com.system.consultationroom.service.impl;

import com.system.consultationroom.bean.ConsultationRoom;
import com.system.consultationroom.bean.OptionBean;
import com.system.consultationroom.bean.OptionsBean;
import com.system.consultationroom.feign.DeptServiceFeign;
import com.system.consultationroom.repository.ConsultationRoomRepository;
import com.system.consultationroom.service.ConsultationRoomService;
import com.system.consultationroom.vo.ConsulationRoomItemVO;
import com.system.consultationroom.vo.DepartItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: ConsultationRoomServiceImpl
 * @description: TODO 类描述
 * @author: Li Chuanwei
 * @date: 2023/11/30 15:40
 * @Company: Copyright [日期] by [作者或个人]
 **/

@Service
public class ConsultationRoomServiceImpl implements ConsultationRoomService {

    @Autowired
    private ConsultationRoomRepository consultationRoomRepository;

    @Autowired
    private DeptServiceFeign deptServiceFeign;

    @Override
    public boolean saveConsultationRoom(ConsultationRoom consultationRoom) {
        ConsultationRoom obj = consultationRoomRepository.save(consultationRoom);
        if(obj != null){
            return true;
        }
        return false;
    }

    @Override
    public boolean removeConsultationRoomById(int id) {
        if (consultationRoomRepository.existsById(id)) {
            consultationRoomRepository.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public boolean modifyConsultationRoom(ConsultationRoom consultationRoom) {
        ConsultationRoom obj = consultationRoomRepository.save(consultationRoom);
        if(obj != null){
            return true;
        }
        return false;
    }

    @Override
    public ConsultationRoom getConsultationRoomById(int id) {
        if (consultationRoomRepository.existsById(id)) {
            return consultationRoomRepository.getReferenceById(id);
        }
        return null;
    }

    @Override
    public List<ConsultationRoom> listAllConsultationRooms() {
        return consultationRoomRepository.findAll();
    }

    @Override
    public List<ConsulationRoomItemVO> getConsulationRoomItemVO(Integer departID) {
        List<ConsultationRoom> consultationRoomList = consultationRoomRepository.findAllByDepartID(departID);
        List<ConsulationRoomItemVO> consulationRoomItemVOList= new ArrayList<>();
        for (ConsultationRoom c: consultationRoomList) {
            ConsulationRoomItemVO cVO = new ConsulationRoomItemVO();
            cVO.setLabel(c.getCrName());
            cVO.setValue(c.getConsultationRoomId());  // 假设 Depart 类中有获取 ID 的方法
            consulationRoomItemVOList.add(cVO);
        }
        return consulationRoomItemVOList;
    }

    @Override
    public OptionsBean<OptionBean> getConsulationRoomOptions() {
        OptionsBean<OptionBean> optionsBean = new OptionsBean<>();
        //options中的data
        List<OptionBean> optionList = new ArrayList<>();
        //查询出科室列表
        List<DepartItemVO> departItemVOList = deptServiceFeign.getDepartItemVOList();
        //对于每一个科室去设置他的option  包括label value  childen
        for(DepartItemVO vo : departItemVOList){
           OptionBean option = new OptionBean();
           option.setValue(Long.valueOf(vo.getValue()));
           option.setLabel(vo.getLabel());
           List<ConsultationRoom> consultationRoomList = consultationRoomRepository.findAllByDepartID(vo.getValue());
           List<OptionBean> consulationRoomOption= new ArrayList<>();
           for (ConsultationRoom c: consultationRoomList) {
                OptionBean coption = new OptionBean();
                coption.setLabel(c.getCrName());
                coption.setValue(Long.valueOf(c.getConsultationRoomId()));
                consulationRoomOption.add(coption);
           }
           option.setChildren(consulationRoomOption);
           optionList.add(option);
        }
        optionsBean.setCode("00000");
        optionsBean.setMsg("ok");
        optionsBean.setData(optionList);
        return optionsBean;
    }
}
